package com.redoor.app.stake.workflow.configurer;

/**
 * Created by wangjunlei on 2018/3/20.
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {

//    @Override
//    public void configureMessageBroker(MessageBrokerRegistry config) {
//        config.enableSimpleBroker("/websocket/topic"); // 客户端订阅地址的前缀
//        config.setApplicationDestinationPrefixes("/app"); // 客户端请求服务端的前缀
//    }
//
//    @Override
//    public void registerStompEndpoints(StompEndpointRegistry registry) {
//        registry.addEndpoint("/websocket/my-websocket") // 客户端连接服务端的端点
//                .withSockJS(); // 开启SockJS支持
//    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler(), "/workorder/websocket")
                .addInterceptors(myInterceptor()).setAllowedOrigins("*"); //url和handler的mapping
    }

    @Bean
    public WebSocketHandler webSocketHandler(){
        return new WorkOrderWebSocketHandler();
    }

    //直接注入自己定义的websocket拦截器
    @Bean
    public WebSocketHandshakeInterceptor myInterceptor(){
        return new WebSocketHandshakeInterceptor();
    }

}
